﻿using System;
using System.Web;
using Jayden.Dll.Core.Translations;
using Jayden.Dll.Web.Enums;
using Jayden.Dll.Web.HtmlControls.Descriptions;

namespace Jayden.Dll.Web.HtmlControls.Generics
{
	public class HtmlComment : HtmlControl
	{
		private string m_Comment;
		public HtmlComment(string text = "", bool htmlEncode = false)
			: base(HtmlCommentDescription.Instance)
		{
			SetComment(text, htmlEncode);
		}

		public void SetComment(string text, bool htmlEncode = false)
		{
			if (htmlEncode)
				m_Comment = HttpUtility.HtmlEncode(text);
			m_Comment = text;
		}
		public string GetComment(bool htmlDecode = false)
		{
			if (htmlDecode)
				return HttpUtility.HtmlDecode(m_Comment);
			return m_Comment;
		}
		public string Comment { get { return GetComment(true); } set { SetComment(value, true); } }

		public override void ProcessHtmlRequest(HttpHandlerContext context) { }
		public override void ProcessJsonRequest(HttpHandlerContext context) { }
		public override void Render(System.Web.UI.HtmlTextWriter writer, RenderMode mode, int level, bool innerHtml = false)
		{
			if (innerHtml)
				return;
			if (mode != RenderMode.Production)
			{
				for (int c = 0; c < level; c++)
					writer.Write('\t');
				if (level > 0 && mode == RenderMode.Debug)
					writer.Write("-->");
			}
			writer.Write("<!--" + m_Comment + "-->");
			if (level != 0 && (mode == RenderMode.Debug || mode == RenderMode.XHtml))
			{
				if (mode == RenderMode.Debug)
					writer.Write("<!--");
				writer.WriteLine();
			}
		}
		public override string ToString() { return m_Comment; }

		public override void Load(System.Xml.XmlNode node, HtmlControlLoader loader) { m_Comment = node.InnerText; }

		public override void TranslateControl(ITranslator translator) { }

	}
	public class HtmlCommentDescription : HtmlControlDescription
	{
		public static HtmlCommentDescription Instance = new HtmlCommentDescription();
		private HtmlCommentDescription() { }
		public override Type ControlType { get { return typeof(HtmlComment); } }
	}
}
